package com.hurui.xiaoming.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.adapter.ContarctAdapter;
import com.hurui.xiaoming.bean.Li_ContractResult;
import com.hurui.xiaoming.utils.DefultData;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyContractActivity extends BaseActivity {

    private ImageView back_iv;
    private TextView title_tv;
    Map map = new HashMap();
    XRecyclerView creaditor_rv;

    List list = new ArrayList();
    ContarctAdapter adapter;

    private static int INIT_PAGE = 1;
    int page = INIT_PAGE;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_contract);

        initView();
        initAction();

    }

    @Override
    protected void onStart() {
        super.onStart();
        getData(page);
    }

    @Override
    public void initView() {
        back_iv = (ImageView) findViewById(R.id.back_iv);
        title_tv = (TextView) findViewById(R.id.top_title_tv);
        title_tv.setText("我的合同");


        creaditor_rv = (XRecyclerView) findViewById(R.id.creaditor_rv);
        creaditor_rv.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL,false));
        adapter = new ContarctAdapter(mContext,list);
        creaditor_rv.setAdapter(adapter);
    }

    @Override
    public void initAction() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onFinish();
            }
        });

        adapter.setOnItemClickListener(new ContarctAdapter.OnItemClickListener() {

            @Override
            public void onItemJieKuanClick(View view, Li_ContractResult.DataBean dataBean) {
                Intent intent = new Intent(MyContractActivity.this,ContractInfoActivity.class);
                intent.putExtra("databean",dataBean);
                intent.putExtra("contract","2");
                startActivity(intent);
            }

            @Override
            public void onItemJuJianClick(View view, Li_ContractResult.DataBean dataBean) {
                Intent intent = new Intent(MyContractActivity.this,ContractInfoActivity.class);
                intent.putExtra("databean",dataBean);
                intent.putExtra("contract","1");
                startActivity(intent);
            }

            @Override
            public void onItemShouJuClick(View view, Li_ContractResult.DataBean dataBean) {
                Intent intent = new Intent(MyContractActivity.this,ContractInfoActivity.class);
                intent.putExtra("databean",dataBean);
                intent.putExtra("contract","3");
                startActivity(intent);
            }
        });

        creaditor_rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getData(INIT_PAGE);
            }

            @Override
            public void onLoadMore() {
                page++;
                getData(page);
            }
        });
    }

    /**
     * 获取网络数据
     */
    public void getData(int page){
        map.clear();
        map.put("ui_id", WriteReadSharedPrefs.readUserID(mContext)+"");
        map.put("page_num",page+"");
        Call<Li_ContractResult> call = apiSevice.onHttpPostGetContractList(DefultData.getContractList,map);
        call.enqueue(new Callback<Li_ContractResult>() {
            @Override
            public void onResponse(Call<Li_ContractResult> call, Response<Li_ContractResult> response) {
                Li_ContractResult li_contractResult = null;
                if(response.isSuccessful()){
                    li_contractResult = response.body();
                    List<Li_ContractResult.DataBean> data = li_contractResult.getData();
                    if(data != null){
                        adapter.refreshData(data);
                    }
                    creaditor_rv.refreshComplete();
                    creaditor_rv.loadMoreComplete();
                }else{
                    onToast("获取合同列表失败");
                }
            }

            @Override
            public void onFailure(Call<Li_ContractResult> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
                creaditor_rv.refreshComplete();
                creaditor_rv.loadMoreComplete();
            }
        });
    }
}
